package com.xj.zhihudemo.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xj.zhihudemo.R;
import com.xj.zhihudemo.mode.StoryDetailInfo;
import com.xj.zhihudemo.retrofit.ApiClient;
import com.xj.zhihudemo.retrofit.ApiStore;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.xj.zhihudemo.R.id.block_recommenders;


/**
 * Created by Administrator on 2016/9/29.
 */

public class StoryFragment extends Fragment {
    private final String TAG = getClass().getSimpleName();
    @BindView(R.id.sd_news_img)
    SimpleDraweeView mSdNewsImg;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_img_source)
    TextView mTvImgSource;
    @BindView(R.id.block_story_img)
    FrameLayout mBlockStoryImg;
    @BindView(block_recommenders)
    LinearLayout mBlockRecommenders;
    @BindView(R.id.wv_news)
    WebView mWvNews;
    @BindView(R.id.scrollView)
    ScrollView mScrollView;
    private StoryDetailInfo mStoryDetailInfo;
    private int storyNewsID;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        View layout = inflater.inflate(R.layout.fragment_story_detail, container,
                false);

        return layout;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onViewCreated");
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {

            //when this fragment is visible to user
            if (mStoryDetailInfo == null) {
                storyNewsID = getArguments().getInt("news_id");
                Log.e(TAG + "新闻详情", storyNewsID + "");
                getData(storyNewsID);

            } else {
                refresh(mStoryDetailInfo);
            }
        }
    }

    private void getData(int storyNewsID) {
        String newsID = String.valueOf(storyNewsID);
        ApiStore apiStore = ApiClient.retrofit().create(ApiStore.class);
        final Call<StoryDetailInfo> storyDetailCall = apiStore.getStrory(newsID);
        storyDetailCall.enqueue(new Callback<StoryDetailInfo>() {
            @Override
            public void onResponse(Call<StoryDetailInfo> call, Response<StoryDetailInfo> response) {
                Log.d(TAG, "success");
                mStoryDetailInfo = response.body();
                if (mStoryDetailInfo != null) {
                    refresh(mStoryDetailInfo);
                }
            }

            @Override
            public void onFailure(Call<StoryDetailInfo> call, Throwable t) {
                Log.e(TAG, "error");

            }
        });
    }

    private void refresh(StoryDetailInfo storyDetailInfo) {

        if (null!=storyDetailInfo.getImages()) {
            mSdNewsImg.setImageURI(storyDetailInfo.getImages().get(0));
        }else {
            mBlockStoryImg.setVisibility(View.GONE);
        }
        mSdNewsImg.setColorFilter(R.color.translucent_status);
        mTvTitle.setText(storyDetailInfo.getTitle());
//        mTvImgSource.setText(storyDetailInfo.geti);
        if (storyDetailInfo.getRecommenders() == null) {
            mBlockRecommenders.setVisibility(View.GONE);
        } else {
            mBlockRecommenders.removeViews(1, mBlockRecommenders.getChildCount() - 1);
            for (StoryDetailInfo.RecommendersBean rec : storyDetailInfo.getRecommenders()) {
                SimpleDraweeView avatar = (SimpleDraweeView) View.inflate(getActivity(), R.layout.list_item_recommender, null);
//                ImageUtil.displayImage(rec.avatar, avatar);
                avatar.setImageURI(rec.getAvatar());
                mBlockRecommenders.addView(avatar);
            }
        }

        String Css = "";
        for (String css_url : storyDetailInfo.getCss()) {
            Css += "<link rel=\"stylesheet\" href=" + css_url + ">\n";
        }
        String JS = "";
        for (String js_url :storyDetailInfo.getJs()) {
            JS += "<script src=" + js_url + "/>\n";
        }
        String Body = storyDetailInfo.getBody().replaceAll("<div class=\"img-place-holder\"></div>","");
        StringBuilder builder=new StringBuilder();
        builder.append("<html>\n")
                .append("<head>\n")
                .append(Css).append(JS)
                .append("</head>\n")
                .append("<body>")
                .append(Body)
                .append("/<body>\n")
                .append("</html>");
      mWvNews.loadData(builder.toString(),"text/html;charset=UTF-8","UTF-8");
    }
}
